import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * 系统管理--模板维护
 * @date 2019-6-26

 */
import { createPagination, getResponse } from 'utils/utils';
import { queryUnifyIdpValue } from 'cpaas-front/lib/services/api';
import { createTemplate as _createTemplate, editTemplate as _editTemplate, fetchTemplates as _fetchTemplates } from "../services/templatesService";
export default {
  namespace: 'cpfmTemplate',
  state: {
    templateData: {},
    pagination: {}
  },
  effects: {
    fetchEnum: /*#__PURE__*/_regeneratorRuntime.mark(function fetchEnum(_, _ref) {
      var call, put, res, dataTenantLevel;
      return _regeneratorRuntime.wrap(function fetchEnum$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryUnifyIdpValue, 'CPFM.DATA_TENANT_LEVEL');

            case 3:
              res = _context.sent;
              dataTenantLevel = getResponse(res);

              if (!dataTenantLevel) {
                _context.next = 8;
                break;
              }

              _context.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  dataTenantLevel: dataTenantLevel
                }
              });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, fetchEnum);
    }),
    fetchTemplates: /*#__PURE__*/_regeneratorRuntime.mark(function fetchTemplates(_ref2, _ref3) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchTemplates$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(_fetchTemplates, payload);

            case 4:
              res = _context2.sent;
              list = getResponse(res);

              if (!list) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  templateData: list,
                  pagination: createPagination(list)
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchTemplates);
    }),
    // 新建保存
    createTemplate: /*#__PURE__*/_regeneratorRuntime.mark(function createTemplate(_ref4, _ref5) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createTemplate$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call;
              _context3.next = 4;
              return call(_createTemplate, _objectSpread({}, payload));

            case 4:
              res = _context3.sent;
              return _context3.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, createTemplate);
    }),
    // 编辑保存
    editTemplate: /*#__PURE__*/_regeneratorRuntime.mark(function editTemplate(_ref6, _ref7) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function editTemplate$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_editTemplate, _objectSpread({}, payload));

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, editTemplate);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};